package com.bonc.excel.configuration;

import com.bonc.excel.utils.LocalDateTimeUtils;
import com.bonc.excel.exception.BusinessException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @author wujing
 */
@Slf4j
public class JsonLocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {


    @Override
    public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
            throws IOException {
        String dateAsString = jsonParser.getText();
        if (StringUtils.isBlank(dateAsString)) {
            return null;
        }
        try {
            /*LocalDate date = LocalDate.parse(dateAsString, DateTimeFormatter.ISO_LOCAL_DATE);
            System.out.println(date.atTime(LocalTime.MIN));*/
            return LocalDateTime.parse(dateAsString, LocalDateTimeUtils.FORMATTER);
        } catch (Exception e) {
            log.error("deserialize LocalDateTime error",e);
            throw new BusinessException(e);
        }
    }


}